\documentclass[a4paper,10pt,turkish,onecolumn,openany,twoside]{memoir}
\usepackage{graphicx}

\include{header}


\begin{document}
%\fontfamily{ppl}\selectfont

\title{Peyderpey}
\author{ }
\date{ }
%\maketitle
%\thispagestyle{empty}
%\newpage

\pagenumbering{roman}
\begin{KeepFromToc}
\tableofcontents
\end{KeepFromToc}
\newpage
\pagenumbering{arabic} 

%\pagestyle{plain}

\chapter{Basics}
\section{Getting Started}
\markboth{\thetitle}{}

This section serves as an introduction to the MATLAB environment and programming language as well as to this tutorial. It describes how to configure and start using MATLAB, search for help, and how to perform file input and output. \index{memory} bulk index.

\subsection{What is MATLAB?}

MATLAB is a programming environment as well as a high level, interpreted, dynamically typed language, supporting functional, object oriented, and event driven paradigms. It is well suited for numerical computation, particularly computations involving matrix operations and linear algebra.

MATLAB has excellent support for data visualization and its concise and expressive syntax, as well as the plethora of predefined functions, results in a powerful environment excellent for rapid prototyping with minimal overhead.

Yet, MATLAB is not just a scripting language for quick and dirty calculations. Recent versions have seen a dramatic increase for the support of large scale, highly structured code to rival C++, Java and the like. If you want the best of both worlds, MATLAB's integrated java support lets you create and manipulate instances of java classes right in your MATLAB programs. You can also call out to code written in C, C++, Perl, Fortran, or execute dos or unix system commands. Furthermore, MATLAB code can be exported for use in Java, C, C++, and .Net programs, or as a stand alone application, mitigating its primary disadvantage - that it is not free.

As an interpreted language, some operations are slower than in a compiled language, particularly those involving loops. This too has improved in recent versions. In many cases, however, loops can be \textit{vectorized} resulting in very quick code that invokes low level, highly optimized, \textit{compiled} functions from the MATLAB library. When the most computationally expensive parts of your program can be written this way, the speed of MATLAB code rivals that of compiled languages.

Finally, with the addition of various toolkits, e.g. for Statistics, Bioinformatics, Optimization, or Image Processing, to name just a few, the MATLAB environment can be extended for more specialized requirements.

MATLAB\textregistered\ is a product of the The Mathworks™ company; for more general information, consult their website at http://www.mathworks.com/.


\subsection{Starting MATLAB}
To launch MATLAB double click on the MATLAB icon or type \texttt{MATLAB} at a command prompt. You should get a display something like the one below

\begin{figure}[htbp]
	\centering
	\includegraphics[width=0.6\textwidth]{command_window.png}
	\caption{Initial MATLAB IDE view}
	\label{fig:command_window}
\end{figure}

You can immediately type things in to the command window and compute their value


\lstset{label=kod:ornek}
\begin{matlabcode}
1 + 1 % arithmetic
sqrt(pi) % more arithmetic
'foo' % a string
or(true, false) % a boolean value
randn(2,2) % a 2d real matrix
xs = 1:10; plot(xs, xs .^ 2, 'o-'); % a 1d plot
\end{matlabcode}

\lstset{label=}
\begin{matlabresult}
ans =
     2
ans =
    1.7725
ans =
foo
ans =
     1
ans =
    1.3005   -0.1551
    0.2691    0.0342
\end{matlabresult}

\begin{figure}[htbp]
	\centering
	\includegraphics[width=0.5\textwidth]{square_func.png}
	\caption{XXX}
	\label{fig:square_func}
\end{figure}

We explain the built-in functions and data types in more detail below.

Typing matlab -nojvm starts Matlab without the Java virtual machine, significantly reducing memory requirements at the cost of reduced functionality. None of the Matlab desktop windows are available in this mode, except for the command window. Here is how it looks.


\begin{figure}[htbp]
	\centering
	\includegraphics[width=0.6\textwidth]{command_window_nojvm.png}
	\caption{XXX}
	\label{fig:command_window_nojvm}
\end{figure}

\subsection{Variables}
We can create named \index{memory} locations to store data, called variables, very easily in Matlab.

\begin{matlabcode}
myvar = 3
a   = 'hello'
f   = log10(100)
g   = 2^10 - 1	
\end{matlabcode}

\begin{matlabresult}
myvar =
     3
a =
hello
f =
     2
g =
        1023
\end{matlabresult}

Variable names must \index{begin} with a character and can contain up to 63 characters on most systems.

Matlab stands for "matrix laboratory", since it was originally designed as a front-end to various numerical linear \index{algebra} packages. Consequently it is easy to create vectors and matrices. For example,

\begin{matlabcode}
a = 1:3 % 1d vector
b = [1 2; 3 4] % 2d matrix
c = ones(2,2,2) % 3d array
\end{matlabcode}

\begin{matlabresult}
a =
     1     2     3
b =
     1     2
     3     4
c(:,:,1) =
     1     1
     1     1
c(:,:,2) =
     1     1
     1     1
\end{matlabresult}
XXX

\subsection{Workspace}\
\subsection{Command Window}\
\subsection{Help}\
\subsection{Other Matlab resources}\


\section{Matrix Operations}\ 
\subsection{Creating Matrices}\
\subsection{The Size of a Matrix}\
\subsection{Transposing a Matrix}\
\subsection{Sums and Means}\
\subsection{Concatenating Matrices}
\subsection{Basic Indexing}\
\subsection{Logical Indexing}
\subsection{Assignment}
\subsection{Deletion}\ 
\subsection{Expansion}\
\subsection{Linear Indexing}\
\subsection{Reshaping and Replication}\
\subsection{Element-Wise Matrix Arithmetic}\
\subsection{Matrix Multiplication}\
\subsection{Solving linear systems}\ 
\subsection{More Linear Algebra}\
\subsection{Multidimensional Arrays}\ 
\subsection{Sparse Matrices}\
\subsection{Other numeric data types}\
\subsection{Other Useful Functions}\



\section{Functions}\ 
\subsection{Why use functions?}\ 
\subsection{Creating Functions}\
\subsection{Comments}\
\subsection{Multiple Functions Per File}\ 
\subsection{Nesting Functions}\ 
\subsection{Recursive Functions}\ 
\subsection{Function Handles / Anonymous Functions}\ 
\subsection{Function composition}\ 
\subsection{Variable number of Input Arguments}\ 
\subsection{Variable number of Output Arguments}\ 
\subsection{Matlab path}\ 

\section{Chol and Qr tricks}
\section{QR update}

The update works in $O(n^2)$.
\begin{matlabcode}
mu = sqrt(eps);
n = 3;
A = [ones(1,n); mu*eye(n)];
[Q,R] = qr(A);
u = [-1 zeros(1,n)]'; v = ones(n,1);

tic
[QT,RT] = qr(A + u*v');
toc

tic
[Q1,R1] = qrupdate(Q,R,u,v);
toc
\end{matlabcode}

\section{Cholesky update}
Works in $O(n^2)$
\begin{matlabcode}
clc, clear
n = 3;

B = 1/log(n)*randn(n);
A = B*B';
R = chol(A);

tic
iA = inv(A)
toc

tic
iR = inv(R);
iRt = iR';
iA2 = iR*iRt
toc

tic
A3 = R'*R;
iA3 = inv(A3)
toc
\end{matlabcode}

\section{Sherman Morrison}
Hızlı güncelleme. Woodbury'ye de bak.
http://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula

\begin{matlabcode}
clc, clear

n = 3;
d = 2;
rng(1)
r = randn(d,n);
A = r*r';
x = (1:d)';
iA = inv(A);
R = chol(A);

tic
B = A + x*x';
iB = inv(B)
toc

tic
iB2 = iA - (iA*x*x'*iA)./(1+x'*iA*x)
toc

tic
R = cholupdate(R,x);
iR = inv(R);
iRt = iR';
iB3 = iR*iRt
toc
\end{matlabcode}

\onecolindex
\printindex
\end{document}